<?php
declare(strict_types=1);

namespace App\Site\Service;

use App\Site\Model\SitePublicMenu;
use App\Site\Service\Input\PublishMenuInput;
use App\Site\Service\Query\PublicMenuQuery;
use App\System\Mapper\SystemMenuMapper;
use App\System\Model\SystemMenu;
use App\System\Service\SystemMenuService;
use Deepwell\EasyQuery\Concern\QueryService;
use Hyperf\Collection\Collection;
use Hyperf\Di\Annotation\Inject;
use Mine\Abstracts\AbstractService;
use Mine\Annotation\Transaction;
use Mine\MineModel;

/**
 * 公共菜单服务类
 * @method SitePublicMenu queryOne(?PublicMenuQuery $input = null)
 * @method Collection<int, SitePublicMenu> queryList(?PublicMenuQuery $input = null)
 */
class SitePublicMenuService extends AbstractService
{
    use QueryService;

    #[Inject]
    protected SystemMenuMapper $systemMenuMapper;

    public function __construct()
    {
        $this->queryModelClassName = SitePublicMenu::class;

    }


    #[Transaction]
    public function publish(PublishMenuInput $input): void
    {
        $input->menu_id_list->each(function ($menuId) {
            $getMenu = new PublicMenuQuery;
            $getMenu->menu_id = $menuId;
            $menu = $this->queryOne($getMenu);
            if (!$menu->id) {
                $menu->menu_id = $menuId;
                $menu->save();
            }
        });
    }

    #[Transaction]
    public function unPublish(PublishMenuInput $input): void
    {
        $input->menu_id_list->each(function ($menuId) {
            $getMenu = new PublicMenuQuery;
            $getMenu->menu_id = $menuId;
            $menu = $this->queryOne($getMenu);
            if ($menu->id) {
                $menu->delete();
            }
        });
    }

    public function getMenuList(): array
    {
        $list = SystemMenu::where('status', MineModel::ENABLE)->orderBy('sort', 'desc')->get()->toArray();

        $this->renderItems($list, null, 2);

        return $list;
    }

    protected function renderItems(array &$items, ?array $publishList = null, int $mode = 1): void
    {
        // 获取已发布的菜单
        if (is_null($publishList)) {
            $getList = new PublicMenuQuery();
            $publishList = $this->queryList($getList)->pluck('menu_id')->toArray();
        }
        if (empty($publishList)) {
            return;
        }

        foreach ($items as $key => &$menu) {
            $isPublish = in_array($menu['id'], $publishList);
            switch ($mode) {
                case 1:
                    $menu['is_publish'] = intval($isPublish);
                    break;
                case 2:
                    if (!$isPublish) {
                        unset($items[$key]);
                        continue 2;
                    }
                    break;
                default:
            }
            // 递归子菜单
            if (!empty($menu['children'])) {
                $this->renderItems($menu['children'], $publishList, $mode);
            }
        }

        $items = array_values($items);
    }

    public function getTree($params)
    {
        $mode = $params['mode'] ?? 1;
        $menus = container()->get(SystemMenuService::class)->getTreeList($params, false);
        $this->renderItems($menus, null, $mode);
        return $menus;
    }

    public function getRoutersByIds(array $ids): array
    {
        $routes = $this->systemMenuMapper->getRoutersByIds($ids);
        $this->renderItems($routes, null, 2);
        return $routes;
    }

    public function getSuperAdminRouters(): array
    {
        $routes = $this->systemMenuMapper->getSuperAdminRouters();
        $this->renderItems($routes, null, 2);
        return $routes;
    }
}